/*
 * Created on May 19, 2005
 *
 */
package org.mushroomdb.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

import org.mushroomdb.exception.RDBMSRuntimeException;


/**
 * Carga las propiedades globales desde un archivo de propiedades
 * y las mantiene cacheades.
 * 
 * @author mchiodi
 *
 */
public class Properties {
	
	private final static String PROPERTIES_FILE = "mushroomdb.properties"; //$NON-NLS-1$
	private static java.util.Properties properties;
	private static Map propertiesMap;
	
	/**
	 * Carga el archivo de propiedades.
	 */
	static {
		Properties.propertiesMap = new HashMap();
		Properties.properties = new java.util.Properties();
		InputStream inputStream = null;
		boolean found = false;
		FileNotFoundException fileNotFoundException = null;
		try {
			
			/* Use system property */
			String propertiesFilePath = System.getProperty(PROPERTIES_FILE);
			if (propertiesFilePath != null && propertiesFilePath.length() > 0) {
				inputStream = new FileInputStream(propertiesFilePath);
				found = true;
			}
			
			/* Search in classpath */
			else {
				String classPath = System.getProperty("java.class.path"); //$NON-NLS-1$
				String pathSeparator = System.getProperty("path.separator"); //$NON-NLS-1$
				String fileSeparator = System.getProperty("file.separator"); //$NON-NLS-1$
				StringTokenizer paths = new StringTokenizer(classPath,pathSeparator); //$NON-NLS-1$
				
				while (paths.hasMoreTokens() && !found) {
					String path = paths.nextToken();
					try {
						inputStream = new FileInputStream(path + fileSeparator + PROPERTIES_FILE);
						found = true;
					} catch (FileNotFoundException e1) {
						fileNotFoundException = e1;
					}
				}
			}
			
			Properties.properties.load(inputStream);
			Properties.loadProperties(properties);
		/*} catch (FileNotFoundException e) {
			throw new RDBMSRuntimeException(
					"The file mushroomdb.properties was not found.", //$NON-NLS-1$
					e);*/
		} catch (IOException e) {
			throw new RDBMSRuntimeException(
					"Error trying to read mushroomdb.properties file.", //$NON-NLS-1$
					e);
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				throw new RDBMSRuntimeException(
						"Error trying to close mushroomdb.properties file.", //$NON-NLS-1$
						e);
			}
			if (!found) {
				throw new RDBMSRuntimeException(
						"The file mushroomdb.properties was not found.", //$NON-NLS-1$
						fileNotFoundException); //$NON-NLS-1$
			}
		}
	}

	/**
	 * Defualt constructor.
	 */
	private Properties() {
	}
	
	public static String getProperty(String propertyName) {
		/** Propiedades cacheadas */
		String value = (String) Properties.propertiesMap.get(propertyName);
		if (value != null) {
			return value;
		} else {
			return Properties.properties.getProperty(propertyName);
		}
	}
	
	/**
	 * Carga las propiedades
	 * @return
	 */
	private static void loadProperties(java.util.Properties properties) {
		Iterator iterator = PropertiesHelper.getProperties();
		while (iterator.hasNext()) {
			String propertyName = (String) iterator.next();
			String value = properties.getProperty(propertyName);
			if (value != null) {
				Properties.propertiesMap.put(propertyName,value);
			} else {
				Properties.propertiesMap.put(propertyName,""); //$NON-NLS-1$
			}
		}
	}
}
